package com.op.teachingliteapp.database.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.op.teachingliteapp.bean.BaseBean;
import com.op.teachingliteapp.database.DBHelper;
import com.op.teachingliteapp.database.MainDBHelper;
import com.op.teachingliteapp.database.table.BaseColumn;

/**
 * Created by Genji on 2015/6/24. Modified by Kevin
 */
public abstract class AbstractDao {
	protected String tb;
	protected SQLiteDatabase db;

	public AbstractDao(Context context) {
		this.db = new MainDBHelper(context).getWritableDatabase();
	}
	
	/**
	 * 参数暂时没用到，dbhelper改成单例，context用全局的，用户名默认从prefs里取
	 * @author Michael
	 * 
	 * @param context
	 * @param userId
	 */
	public AbstractDao(Context context, String userId) {
            this.db = DBHelper.getInstance().getWritableDatabase();

    }

	/**
	 * insert方法
	 * 
	 * @param baseBean
	 * @return
	 */
	public long insert(BaseBean baseBean) {
		return db.insert(tb, null, baseBean.getContentValues());
	}

	/**
	 * update方法
	 * 
	 * @param baseBean
	 * @return
	 */
	public long update(BaseBean baseBean) {
		String where;
		String id = baseBean.getId();
		ContentValues contentValues = baseBean.getContentValues();
		try {
			where = BaseColumn.COLUMN_ID + " = ?";
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

		return db.update(tb, contentValues, where, new String[]{ id });
	}

	/**
	 * delete方法
	 * 
	 * @param baseBean
	 * @return
	 */
	public long delete(BaseBean baseBean) {
		String where;
		String id = baseBean.getId();
		try {
			where = BaseColumn.COLUMN_ID + "= ?";
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

		return db.delete(tb, where, new String[] { id });
	}

	/**
	 * 批量delete方法
	 * 
	 * @param baseBeans
	 * @return
	 */
	public long delete(BaseBean... baseBeans) {
		this.db.beginTransaction();
		try {
			for (BaseBean baseBean : baseBeans) {
				this.delete(baseBean);
			}
			this.db.setTransactionSuccessful();
			this.db.endTransaction();
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			this.db.endTransaction();
			return -1;
		}
	}

	/**
	 * 清空方法
	 * 
	 * @return
	 */
	public long empty() {
		try {
			String sql = "delete from " + tb + ";";
			db.execSQL(sql);
			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			this.db.endTransaction();
			return -1;
		}
	}

	/**
	 * 插入或者更新
	 * 
	 * @return
	 */
	public long insertOrUpdate(BaseBean baseBean) {
		try {
			List<String> ids = getIds();
			if (ids.contains(baseBean.getId())) {
				update(baseBean);
			} else {
				insert(baseBean);
			}

			return 0;
		} catch (Exception e) {
			e.printStackTrace();
			this.db.endTransaction();
			return -1;
		}
	}

	/**
	 * 查找所有的ids
	 * 
	 * @return
	 */
	public List<String> getIds() {
		List<String> ids = new ArrayList<String>();

		String[] columns = { BaseColumn.COLUMN_ID };
		Cursor cursor = db.query(this.tb, columns, null, null, null, null, null);
		while (cursor.moveToNext()) {
			ids.add(cursor.getString(cursor.getColumnIndex(BaseColumn.COLUMN_ID)));
		}

		return ids;
	}
}
